package com.originskyseed.platform.user.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.originskyseed.platform.common.api.util.QueryAppUtil;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.Player;
import com.originskyseed.platform.user.model.PlayerRelateChangeChannel;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IPlayerRelateChangeChannelService;
import com.originskyseed.platform.user.service.IPlayerService;
import com.originskyseed.platform.user.vo.request.PlayerRelateChangeChannelRequestVO;

/**
 * 玩家渠道变化信息表Controller类（OSI_PLAYER_RELATE_CHANGE_CHANNEL）
 * 
 * @author WuZhuoXi
 */
@Controller
@RequestMapping("/playerRelateChangeChannel")
@BaseLocaleResource(localeFileName = "i18n_user")
public class PlayerRelateChangeChannelController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PlayerRelateChangeChannelController.class);

	@DubboReference
	private IPlayerRelateChangeChannelService playerRelateChangeChannelService;

	@DubboReference
	private IPlayerService playerService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateChangeChannelList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<PlayerRelateChangeChannel>> playerRelateChangeChannelList(HttpServletRequest request) {
		BaseResponseBody<BasePage<PlayerRelateChangeChannel>> result = null;
		try {
			// 请求对象
			PlayerRelateChangeChannelRequestVO requestVO = this.getParameterBean(PlayerRelateChangeChannelRequestVO.class);

			// 分页查询
			BasePage<PlayerRelateChangeChannel> page = new BasePage<PlayerRelateChangeChannel>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.playerRelateChangeChannelService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<PlayerRelateChangeChannel>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<PlayerRelateChangeChannel>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateChangeChannelSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<PlayerRelateChangeChannel> playerRelateChangeChannelSave(HttpServletRequest request) {
		BaseResponseBody<PlayerRelateChangeChannel> result = null;
		try {
			// 获取当前用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 请求对象
			PlayerRelateChangeChannel playerRelateChangeChannel = this.getParameterBean(PlayerRelateChangeChannel.class);

			if (StringUtils.isNotBlank(playerRelateChangeChannel.getId())) {
				this.playerRelateChangeChannelService.updateByPrimaryKeySelective(playerRelateChangeChannel);
			} else {


				// 查询玩家信息,工会id
				Player queryPlayer = this.playerService.selectByPlayerId(playerRelateChangeChannel.getPlayerId());
				String groupId = "";
				if (queryPlayer != null) {
					// 接口请求
					TreeMap<String, Object> paramMap = new TreeMap<>();
					paramMap.put("channelId", queryPlayer.getChannelSubId());
					Map map = QueryAppUtil.query("queryUnionUrl", 1, paramMap);
					Integer code = Integer.parseInt(map.get("code").toString());
					if (code == 1) {
						String data = (String) map.get("data");
						JSONObject jsonObject = JSONObject.parseObject(data);
						groupId = jsonObject.getString("tradeUnionId");
					}
				}
				// 接口请求
				TreeMap<String, Object> paramMap = new TreeMap<>();
				paramMap.put("vvId", playerRelateChangeChannel.getPlayerId());
				paramMap.put("newScId", playerRelateChangeChannel.getChannelSubId());
				paramMap.put("segmentingLine", playerRelateChangeChannel.getChangeChannelTime());
				paramMap.put("recordType", 1);
				paramMap.put("opType", 1);
				paramMap.put("tieUpReason", playerRelateChangeChannel.getReason()==null?"推广平台同公会改绑改绑":playerRelateChangeChannel.getReason());
				paramMap.put("groupId", groupId);
				paramMap.put("loginName", userInfo.getName());
				Map map = QueryAppUtil.query("playerChannelUrl", 2, paramMap);
				Integer code = Integer.parseInt(map.get("code").toString());
				if (code == 1) {
					playerRelateChangeChannel.setCreator(userInfo.getName());
					playerRelateChangeChannel.setCreatorId(userInfo.getId());
					this.playerRelateChangeChannelService.insertPlayerRelateChangeChannel(playerRelateChangeChannel);

					result = new BaseResponseBody<PlayerRelateChangeChannel>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), playerRelateChangeChannel);
				}
				else
				{
					String msg="";
					if(map.containsKey("message"))
					{
						msg=map.get("message").toString();
					}
					result = new BaseResponseBody<PlayerRelateChangeChannel>(true, CommonResponseCode.SUCCESS.getCode(),msg, playerRelateChangeChannel);
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<PlayerRelateChangeChannel>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateChangeChannelDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> playerRelateChangeChannelDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.playerRelateChangeChannelService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}